package com.qm.console.controller.rest;

import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.pubdef.dto.withdraw.WithdrawUpdateAdminDTO;
import com.qm.console.service.withdraw.WithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("withdraw")
@Api(value = "[ 控制台:提现管理 ]", description = "提现相关API")
public class WithdrawController {
    @Autowired
    private WithdrawService WithdrawService;

    @RequiresPermissions("withdraw:view")
    @ApiOperation(value = "提现列表", notes = "提现列表内增加显示'处理原因'、'昵称'")
    @GetMapping("/list")
    public BaseRsp listWithdrawAdmin(@RequestParam(value = "limit", required = false, defaultValue = "20") String limit,
                                 @RequestParam(value = "page", required = false, defaultValue = "1") String page,
                                 @RequestParam(value = "userId", required = false) String userId,
                                 @RequestParam(value = "status", required = false) String status,
                                 @RequestParam(value = "countryCode", required = false) String countryCode,
                                 @RequestParam(value = "withdrawType", required = false) String withdrawType,
                                 @RequestParam(value = "startTime", required = false) String startTime,
                                 @RequestParam(value = "endTime", required = false) String endTime){
        Map<String, String> paramMap = getGeneralParamMap(limit, page, userId,status, startTime, endTime);
        if(withdrawType != null){
            paramMap.put("withdrawType", withdrawType);
        }
        if(countryCode != null){
            paramMap.put("countryCode", countryCode);
        }
        return WithdrawService.listWithdrawAdmin(paramMap);
    }

    @RequiresPermissions("withdraw:change")
    @PostMapping("/deal")
    public BaseRsp updateWithdrawAdmin(@RequestBody WithdrawUpdateAdminDTO updateDTO){
        return WithdrawService.updateWithdrawAdmin(updateDTO);
    }


    private Map<String, String> getGeneralParamMap(String limit, String page, String userId,String status, String startTime, String endTime){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("pageSize", limit);
        paramMap.put("pageNum", page);
        if(userId != null){
            paramMap.put("userId", userId);
        }
        if(status != null){
            paramMap.put("status", status);
        }
        if(startTime != null){
            paramMap.put("startTime", startTime);
        }
        if(endTime != null){
            paramMap.put("endTime", endTime);
        }
        return paramMap;
    }
}
